package com.ruoyi.utils;

import cn.hutool.json.JSONUtil;
import com.ruoyi.platform.manager.AsyncManager;
import com.ruoyi.platform.manager.factory.AsyncFactory;
import com.ruoyi.platform.system.domain.SysOperLog;
import com.ruoyi.platform.utils.BeanUtils;
import com.ruoyi.platform.utils.RootExceptionUtil;
import com.ruoyi.platform.utils.SecurityUtils;
import com.ruoyi.platform.utils.ServletUtils;
import com.ruoyi.platform.utils.ip.AddressUtils;
import com.ruoyi.platform.utils.ip.IpUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.util.Date;

/**
 * 日志工具类
 */
public class LogUtil {

    /**
     * 保存队列的异常日志
     * @param log 消息的内容
     * @param e 异常信息
     * @param title 日志的标题
     * @param methodName 方法的全路径名+方法名
     */
    public static void saveQueueErrorLog(Object log,Exception e,String title , String methodName){
        String executor = "系统[无用户登录系统]";
        SysOperLog sysOperLog = new SysOperLog();
        try {
            sysOperLog.setOperIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
        }catch (Exception ipE){
            sysOperLog.setOperIp(IpUtils.getIpAddr(null));
        }
        sysOperLog.setErrorMsg(getErrorDetail(e));
        try {
            sysOperLog.setOperLocation(AddressUtils.getRealAddressByIP(IpUtils.getIpAddr(ServletUtils.getRequest())));
        }catch (Exception addressException){
            sysOperLog.setOperLocation("未知");
        }
        sysOperLog.setBusinessType(11);
        sysOperLog.setJsonResult(JSONUtil.toJsonStr(log));
        sysOperLog.setMethod(methodName);
        sysOperLog.setTitle(title);
        sysOperLog.setOperatorType(0);
        sysOperLog.setOperTime(new Date());
        sysOperLog.setStatus(1);
        sysOperLog.setRequestMethod("MQ");
        sysOperLog.setOperName(executor);
        AsyncManager.me().execute(AsyncFactory.recordOper(sysOperLog));
    }

    /**
     * 保存队列日志
     * @param log 消息的内容
     * @param title 日志的标题
     * @param methodName 方法的全路径名+方法名
     */
    public static void saveQueueLog(Object log,String title, String methodName){
        String executor = "系统[无用户登录系统]";
        try {
            executor = SecurityUtils.getUsername();
        }catch (Exception e){

        }
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setTitle(title);
        sysOperLog.setBusinessType(11);
        sysOperLog.setMethod(methodName);
        sysOperLog.setOperatorType(0);
        sysOperLog.setOperName(executor);
        sysOperLog.setOperIp(IpUtils.getIpAddr(ServletUtils.getRequest()));
        sysOperLog.setStatus(0);
        sysOperLog.setRequestMethod("MQ");
        sysOperLog.setOperTime(new Date());
        sysOperLog.setOperParam(JSONUtil.toJsonStr(log));
        AsyncManager.me().execute(AsyncFactory.recordOper(sysOperLog));
    }

    private static String getErrorDetail(Exception e) {
        String opeContent;
        if (BeanUtils.isNotEmpty(RootExceptionUtil.getRootCauseMessage(e))){
            opeContent = RootExceptionUtil.getFullStackTrace(ExceptionUtils.getRootCause(e));
        }else{
            opeContent = RootExceptionUtil.getFullStackTrace(e);
        }
        return opeContent;
    }


}
